package com.ecmcloud.common.utils;

import java.util.List;

import com.ecmcloud.common.dto.Result;
import com.fasterxml.jackson.core.type.TypeReference;

import cn.hutool.core.util.StrUtil;

public class HttpUtil {

	public static String post(Object object, String url, String baseUrl) {
		String post = cn.hutool.http.HttpUtil.post(StrUtil.format("{}{}", baseUrl, url), JsonMapperUtils.toJsonString(object));
		return post;
	}

	public static <T> Result<T> post(Object object, String url, Class<T> tClass, String baseUrl) {
		String jsonString = post(object, url, baseUrl);
		Result<T> r = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<T>>() {});
		return r;
	}

	public static <T> Result<List<T>> postArray(Object object, String url, Class<T> tClass, String baseUrl) {
		String jsonString = post(object, url, baseUrl);
		Result<List<T>> r = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<List<T>>>() {});
		return r;
	}

	public static String get(String url, String baseUrl) {
		return cn.hutool.http.HttpUtil.get(StrUtil.format("{}{}", baseUrl, url));
	}

	public static <T> Result<T> get(String url, String baseUrl, Class<T> tClass) {
		String jsonString = get(url, baseUrl);
		Result<T> r = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<T>>() {});
		return r;
	}

	public static <T> Result<List<T>> getArray(String url, Class<T> tClass, String baseUrl) {
		String jsonString = get(url, baseUrl);
		Result<List<T>> r = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<List<T>>>() {});
		return r;
	}

}
